package com.das.mechanic_base.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3ScreenUtils;

public class X3ScanVinView extends View {

    //相机遮罩框外面的线，阴影区域，滚动线
    private Paint area, line,tPaint;
    //相机遮罩框中间透明区域
    private Rect center;
    //屏幕大小
    private int screenHeight, screenWidth;

    private int lineX = 0;
    //滚动线速度
    private static final int SPEED = 5;

    private int textTop;

    private String textStr;
    private int centerLeft;
    private Bitmap scan_line;

    private Context mContext;
    private int centerTop;
    private Matrix matrix;
    private String textTops;
    private int textTopTips;
    private Paint tpPaint;

    private boolean isHorizontal = true;

    public X3ScanVinView(Context context) {
        super(context);
    }

    public X3ScanVinView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;

        textStr = mContext.getString(R.string.scan_vin_tips);
        textTops = mContext.getString(R.string.scan_vin);

        area = new Paint(Paint.ANTI_ALIAS_FLAG);
        area.setStyle(Paint.Style.FILL);
        area.setColor(Color.parseColor("#000000"));
        area.setAlpha(100);

        tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        tPaint.setColor(Color.WHITE);
        tPaint.setTextSize(X3ScreenUtils.dipToPx(14,context));

        tpPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        tpPaint.setColor(Color.WHITE);
        tpPaint.setTextSize(X3ScreenUtils.dipToPx(24,context));
        Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
        tpPaint.setTypeface( font );
        screenHeight = X3ScreenUtils.getScreenHeight(context);
        screenWidth = X3ScreenUtils.getScreenWidth(context);
        center = getCenterRect(context, 80, 280,true);

        line = new Paint(Paint.ANTI_ALIAS_FLAG);
        line.setStyle(Paint.Style.FILL);
        //line.setColor(Color.parseColor("#86FFE1"));

        scan_line = BitmapFactory.decodeResource(getResources(), R.mipmap.x3_home_vin_line);

        matrix = new Matrix();
    }

    public X3ScanVinView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 更换title
     * @param str
     */
    public void changeText(String str) {
        this.textStr = str;
        invalidate();
    }

    public Rect getCenter(){
        return center;
    }

    public void changeScanRect(boolean isHorizontal) {
        this.isHorizontal = isHorizontal;
        if (isHorizontal) {
            center = getCenterRect(getContext(), 80, 280,isHorizontal);
            scan_line = adjustPhotoRotation(scan_line,-90);
        } else {
            center = getCenterRect(getContext(), 280, 80,isHorizontal);
            scan_line = adjustPhotoRotation(scan_line,90);
        }
        invalidate();
    }

    /**
     * 根据尺寸获取中间区大小
     *
     * @param context
     * @param height
     * @param width
     * @return
     */
    private Rect getCenterRect(Context context, int height, int width,boolean isHorizontal) {
        height = X3ScreenUtils.dipToPx(height,context);
        width = X3ScreenUtils.dipToPx(width,context);
        Rect rect = new Rect();
        centerLeft = (this.screenWidth - width) / 2;
        centerTop = isHorizontal == true ? (this.screenHeight - height) / 2 - height : (this.screenHeight - height) / 2;
        textTop = centerTop - X3ScreenUtils.dipToPx(63,context);
        textTopTips = isHorizontal == true ? centerTop - X3ScreenUtils.dipToPx(93,context) : centerTop - X3ScreenUtils.dipToPx(73,context);
        rect.set(centerLeft, centerTop, centerLeft + width, centerTop + height);
        return rect;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制四周阴影区域（上下左右），注意+1和-1，不设置不显示边框
        canvas.drawRect(0, 0, screenWidth, center.top - 1, area);
        canvas.drawRect(0, center.bottom + 1, screenWidth, screenHeight, area);
        canvas.drawRect(0, center.top - 1, center.left - 1, center.bottom + 1, area);
        canvas.drawRect(center.right + 1, center.top - 1, screenWidth, center.bottom + 1, area);

        //缩放
        //Log.e("SSSS","是否被缩放了："+startScan);
        if (isHorizontal){
            matrix.setTranslate(centerLeft - X3ScreenUtils.dipToPx(7,mContext),centerTop - X3ScreenUtils.dipToPx(10,mContext));
            canvas.drawBitmap(scan_line, matrix, tPaint);
        } else {
            matrix.setTranslate(centerLeft - X3ScreenUtils.dipToPx(10,mContext),centerTop - X3ScreenUtils.dipToPx(8,mContext));
            canvas.drawBitmap(scan_line, matrix, tPaint);
        }


       // 绘制顶部文字
        Rect rectTips = new Rect();
        tpPaint.getTextBounds(textTops, 0, textTops.length(), rectTips);
        int wtips = rectTips.width();
        canvas.drawText(textTops,(this.screenWidth - wtips) / 2,textTopTips,tpPaint);

        // 绘制文字
//        Rect rect = new Rect();
//        tPaint.getTextBounds(textStr, 0, textStr.length(), rect);
//        int w = rect.width();
//        canvas.drawText(textStr,(this.screenWidth - w) / 2,textTop,tPaint);


        //滚动线
        lineX += SPEED;
        if (isHorizontal){
            if (center.left + lineX >= center.right)
            {
                lineX = SPEED;
            }
            LinearGradient linearGradient = new LinearGradient(center.left,center.top,center.left + lineX,center.top,
                    new int[]{Color.parseColor("#10ffffff"),Color.parseColor("#8086FFE1")},
                    new float[]{0.5f,1f}, Shader.TileMode.MIRROR);
            line.setShader(linearGradient);
            canvas.drawRect(center.left,center.top,center.left + lineX,center.bottom,line);
        }else{
            if (center.top + lineX >= center.bottom) {
                lineX = SPEED;
            }
            LinearGradient linearGradient = new LinearGradient(center.left, center.top, center.left , center.top + lineX,
                    new int[]{Color.parseColor("#10ffffff"), Color.parseColor("#8086FFE1")},
                    new float[]{0.5f, 1f}, Shader.TileMode.MIRROR);
            line.setShader(linearGradient);
            canvas.drawRect(center.left, center.top, center.right , center.top+ lineX, line);
        }

        postInvalidate();
    }

    private Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {
        Matrix m = new Matrix();
        m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
        try {
            Bitmap bm1 = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true);
            return bm1;
        } catch (OutOfMemoryError ex) {
        }
        return null;
    }
}
